$(function () {
    var prospect_datagrid = $("#prospect_datagrid");
    var prospect_dialog = $("#prospect_dialog");
    var prospect_form = $("#prospect_form");
    prospect_datagrid.datagrid({
        url: '/client/query4Prospect.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '姓名', width: 100,},
            {field: 'traceNumber', title: '跟踪次数', width: 100,},
            {field: 'lastTime', title: '最后跟踪时间', width: 100,},
            {field: 'qq', title: 'QQ', width: 100,},
            {field: 'tel', title: '电话', width: 100,},
            {field: 'schoolName', title: '学校', width: 100,},
            {
                field: 'intention', title: '意向程度', width: 100, formatter: function (value) {
                return value.name;
            }
            },
            {
                field: 'intentionArea', title: '意向校区', width: 100, formatter: function (value) {
                return value.name;
            }
            },
            {
                field: 'intentionClass', title: '意向班级', width: 100, formatter: function (value) {
                return value.className;
            }
            },
            {field: 'remark', title: '备注', width: 100,}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#prospect_btns",
        sortName: 'postion_time',
        sortOrder: 'desc'
    });
    prospect_dialog.dialog({
        fit: true,
        buttons: "#cancel",
        closed: true,
        onClose: function () {
            prospect_form.form('clear');
        }
    });
    var methodObj = {
        add: function () {
            var row = prospect_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定跟进该客户吗？', function (r) {
                if (r) {
                    $.get("/client/follow.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            prospect_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },

        watch: function () {
            var row = prospect_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $("#prospect_form input").prop('disabled', true);
            $("#prospect_form input").prop('readOnly', true);
            prospect_form.form('load', row
            );
            prospect_dialog.dialog('setTitle', '编辑潜在客户');
            prospect_dialog.dialog('open');
        },
        changeState: function () {
            var row = prospect_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/prospect/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            prospect_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        del: function () {
            var row = prospect_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该潜在客户吗？', function (r) {
                if (r) {
                    $.get("/prospect/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            prospect_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            prospect_datagrid.datagrid('reload');
        },

        save: function () {
            prospect_form.form('submit', {
                url: '/prospect/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        prospect_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            prospect_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
})



